﻿using System;
using System.IO;
using System.Windows.Forms;
using StockAnalyzer.Core;

namespace StockAnalyzer.Factory
{
    static class StockCollectionFactory
    {
        public static StockCollection Make(DateTime from, DateTime to)
        {
            string stockfile = Application.StartupPath + "\\stock.txt";
            if (File.Exists(stockfile))
            {
                var stream = new FileStream(stockfile, FileMode.Open);
                var reader = new StreamReader(stream);
                try
                {
                    var stocks = new StockCollection();
                    string[] items = reader.ReadToEnd().Split(new[] { '\r', ' ', '\n'});
                    foreach (string s in items)
                    {
                        stocks.Append(s);
                    }

                    var graber = new YahooGraber(stocks, from, to);
                    graber.GetHistory();
                    return stocks;
                }
                finally
                {
                    stream.Close();
                }
            }

            return new StockCollection();
        }
    }
}